#include <windows.h>
#include <assert.h>

HANDLE ghHeap;

DWORD WINAPI threadFunc1(LPVOID para) {
	PVOID pMem;
	pMem = HeapAlloc(ghHeap, HEAP_ZERO_MEMORY, 1024);
	assert(pMem);
	
	sprintf(pMem, "thread id: %8u, para: %8u, pMem: %8p\n", GetCurrentThreadId(), para, pMem);
	printf(pMem);

	Sleep(2000);
	HeapFree(ghHeap, 0, pMem);
	return 0;
}

#include <windows.h>
#include <assert.h>

int main(void) {
	// head + thread
	{
		PVOID pMemNew, pMem;
		HANDLE hHeap = HeapCreate(0, 0, 0);
		assert(hHeap);
		ghHeap = hHeap;
		
		pMem = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, 1024);
		assert(pMem);
		sprintf(pMem, "hello world");
		
		pMemNew = HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, pMem, 2048);
		assert(pMemNew);
		pMem = pMemNew;
		
		HeapFree(hHeap, 0, pMem);

		// thread test
		{
			HANDLE threadList[32];
			int i;
			for (i = 0; i < 32; ++i) {
				threadList[i] = _beginthreadex(0, 0, threadFunc1, i, 0, 0);
				assert(threadList[i]);
			}

			for (i = 0; i < 32; ++i) {
				WaitForSingleObject(threadList[i], INFINITE);
				CloseHandle(threadList[i]);
			}
		}
		
		HeapDestroy(hHeap);
	}
	return 0;
}
